// main.go
package main

import (
	"bufio"
	"fmt"
	"net/rpc"
	"os"
	"strconv"
	"strings"

	"gorpctest/common"
)

func connHandler(c *rpc.Client) {

	reader := bufio.NewReader(os.Stdin)
	//buf := make([]byte, 1024)

	for {
		input, _ := reader.ReadString('\n')
		input = strings.TrimSpace(input)

		if input == "quit" {
			return
		}

		inputs := strings.Split(input, " ")

		if len(inputs) == 2 {
			fmt.Println(inputs[0], "&&", inputs[1])

			pam1, _ := strconv.ParseFloat(inputs[0], 32)
			pam2, _ := strconv.ParseFloat(inputs[1], 32)

			var args = common.Args{float32(pam1), float32(pam2)}
			var result = common.Result{}
			err := c.Call("MathService.Add", args, &result)
			if err != nil {
				fmt.Println("调用失败！", err)
			}
			fmt.Println("调用成功！结果：", result.Value)
		}
		continue
	}
}

func main() {
	var client, err = rpc.Dial("tcp", common.DefServer.GetPath())
	if err != nil {
		fmt.Println("连接不到服务器：", err)
		return
	}

	fmt.Println("开始调用！")
	connHandler(client)
}
